<?php

class MDB2_QueryResult extends QueryResult
{

  /**
   *
   * @var MDB2_Result_Common
   */
  protected $result;

  /**
   *
   * @var string
   */
  protected $query;

  /**
   *
   * @var mixed
   */
  protected $data;

  /**
   *
   * @var int
   */
  protected $limit;

  /**
   *
   * @var int
   */
  protected $offset;

  /**
   *
   * @param MDB2_Result_Common $result
   * @param string $query
   * @param array $data
   * @return void
   */
  public function __construct($result, $query, $data = null, $limit = null, $offset = null)
  {
    $this->result = $result;
    $this->query = $query;
    $this->data = $data;
    $this->limit = $limit;
    $this->offset = $offset;

    MDB2_DAO::validate($result, $query);

    DAO::log(array($query, $data, $limit, $offset));
  }

  public function query()
  {
    return $this->query;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#free()
   */
  public function free()
  {
    if (! empty($this->result)) {
      $this->result->free();
      $this->result = null;
    }
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#fetchOne()
   */
  public function fetchOne()
  {
    $return = $this->result->fetchOne();
    MDB2_DAO::validate($return);
    return $return;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#fetchRow($n)
   */
  public function fetchRow($n = null)
  {
    $return = is_null($n)
      ? $this->result->fetchRow(MDB2_FETCHMODE_ASSOC) 
      : $this->result->fetchRow(MDB2_FETCHMODE_ASSOC, $n);
    MDB2_DAO::validate($return);
    return $return;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#fetchCol($n)
   */
  public function fetchCol($n = null)
  {
    $return = is_null($n) 
      ? $this->result->fetchCol() 
      : $this->result->fetchCol($n);
    MDB2_DAO::validate($return);
    return $return;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#fetchAll()
   */
  public function fetchAll()
  {
    $return = $this->result->fetchAll(MDB2_FETCHMODE_ASSOC);
    MDB2_DAO::validate($return);
    return $return;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#numRows()
   */
  public function numRows()
  {
    if (is_int($this->result)) {
      $return = $this->result;
    }
    else {
      $return = $this->result->numRows();
      MDB2_DAO::validate($return);
    }

    return $return;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IQueryResult#numCols()
   */
  public function numCols()
  {
    $return = $this->result->numCols();
    MDB2_DAO::validate($return);
    return $return;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/QueryResult#columnNames()
   */
  public function columnNames()
  {
    $return = $this->result->getColumnNames();
    MDB2_DAO::validate($return);
    return $return;
  }

}

?>